package org.hdl.anima.message;

import java.io.IOException;

import org.hdl.anima.common.io.InputArchive;
import org.hdl.anima.common.io.OutputArchive;
import org.hdl.anima.common.io.Record;

/**
 * 
 * @author qiuhd
 * @since  2014-2-17
 * @version V1.0.0
 */
public class MessageHeader implements Record{
	
	public static final int LENGTH = (Byte.SIZE / 8) * 2  + Integer.SIZE / 8;
	
	private int length;				//包长度
	private byte encrypted;			//压缩:0.没有压缩 1.压缩
	private byte type;				//包类型
	
	public MessageHeader() {
		
	}

	public MessageHeader(int length,byte encrypted,byte type) {
		this.type = type;
		this.length = length;
		this.encrypted = encrypted;
	}
	
	public void setEncrypted(boolean encrypted) {
		if(encrypted) {
			this.encrypted = 1;
		}else {
			this.encrypted = 0;
		}
	}
	
	public boolean isEncrypted() {
		if (this.encrypted == 1) {
			return true;
		}
		return false;
	}
	
	public byte getType() {
		return this.type;
	}
	
	public void setType(byte type) {
		this.type = type;
	}
	
	public void setLength(int length) {
		this.length = length;
	}
	
	public int getLength() {
		return this.length;
	}

	@Override
	public void serialize(OutputArchive archive) throws IOException {
		archive.writeInt(length);
		archive.writeByte(encrypted);
		archive.writeByte(type);
	}

	@Override
	public void deserialize(InputArchive archive) throws IOException {
		this.length = archive.readInt();
		this.encrypted = archive.readByte();
		this.type = archive.readByte();
	}
}

